package com.ninong.ker.admin.rest;

import com.ninong.ker.admin.service.dto.UserDTO;
import com.ninong.ker.common.tools.Constants;
import com.ninong.ker.common.tools.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.admin.domain.User;
import com.ninong.ker.admin.sevice.UserService;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;

/**
 * 系统部门
 *
 * @author cmd
 */
@RestController
@RequestMapping("/cmd/v1/user")
public class UserResources {

    @Autowired
    private UserService userService;


    @Log("用户列表")
    @CmdAdmin({"admin", "user:list"})
    @PostMapping
    public ResponseEntity<CmdResponse> list(@RequestBody UserDTO entity) {
        CmdResponse response = new CmdResponse();
        response.setData(userService.listPage(entity));
        return ResponseEntity.ok(response);
    }

    @Log("用户删除")
    @CmdAdmin({"admin", "user:delete"})
    @DeleteMapping
    public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids) {
        userService.delete(ids);
        CmdResponse response = new CmdResponse();
        String mess = PropertyUtil.getProperty("DELETE_SUCCESS");
        response.setMessage(mess);
        return ResponseEntity.ok(response);
    }

    @Log("用户新增或编辑")
    @CmdAdmin({"admin", "user:add", "user:update"})
    @PutMapping
    public ResponseEntity<CmdResponse> create(@RequestBody User entity) throws Exception {
        userService.create(entity);
        return ResponseEntity.ok(new CmdResponse());
    }

    @Log("用户详情")
    @GetMapping(value = "/{id}")
    public ResponseEntity<CmdResponse> get(@PathVariable String id) {
        CmdResponse response = new CmdResponse();
        response.setData(userService.get(id));
        return ResponseEntity.ok(response);
    }

    @Log("激活禁用用户")
    @PostMapping(value = "/enable/{id}/{enable}")
    public ResponseEntity<CmdResponse> enableUser(@PathVariable String id, @PathVariable Boolean enable) {
        CmdResponse response = new CmdResponse();
        userService.enableUser(id, enable);
        return ResponseEntity.ok(response);
    }

    /**
     * 用户树形集合
     *
     * @return
     */
    @GetMapping(value = "/userTree")
    public ResponseEntity<CmdResponse> listUserTree() {
        CmdResponse response = new CmdResponse();
        response.setData(userService.listUserTree());
        return ResponseEntity.ok(response);
    }
    
    @Log("重置用户登录密码")
    @PostMapping(value = "/resetPassword/{id}")
    public ResponseEntity<CmdResponse> resetPassword(@PathVariable String id)  {
        CmdResponse response = new CmdResponse();
        userService.resetPassword(id);
        return ResponseEntity.ok(response);
    }
    
    @Log("修改用户密码")
    @PostMapping(value = "/changePassword")
    public ResponseEntity<CmdResponse> changePassword(@RequestBody UserDTO dto, @RequestHeader(value = Constants.TOKEN) String token) throws Exception {
        CmdResponse response = new CmdResponse();
        response.setMessage(userService.changePassword(dto, token));
        return ResponseEntity.ok(response);
    }

}
